package com.nwu.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nwu.common.result.Result;
import com.nwu.common.utills.JwtUtil;
import com.nwu.user.domain.Role;
import com.nwu.user.domain.RoleUser;
import com.nwu.user.domain.User;
import com.nwu.user.domain.dto.LoginDto;
import com.nwu.user.domain.po.LoginUser;
import com.nwu.user.domain.vo.LoginVo;
import com.nwu.user.properties.JwtProperties;
import com.nwu.user.service.LoginService;
import com.nwu.user.service.RoleService;
import com.nwu.user.service.RoleUserService;
import com.nwu.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
@Slf4j
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;
    private final RoleUserService roleUserService;


    @ApiOperation("查询 全部用户")
//    @PreAuthorize("hasAuthority('admin')")
    @GetMapping("/allUser")
    public Result list(){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>()
                .select()
                .ne(User::getId,1);//管理员
        return Result.success(userService.list(wrapper));
    }


    @ApiOperation("查询 条件 分页")
//    @PreAuthorize("hasAuthority('admin')")
    @GetMapping("/condition/{page}/{pageSize}")
    public Result pageList(@PathVariable Integer page, @PathVariable Integer pageSize,String name,String username,String phone,String beginCreateTime,String endCreateTime,String remark){
        Page<User> rowPage = new Page(page, pageSize);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>()
                .select()
                .like(remark!=null,User::getRemark,remark)
                .like(name != null,User::getName,name)
                .like(username!=null,User::getUsername,username)
                .like(phone!=null,User::getPhone,phone)
                .ge(beginCreateTime!=null,User::getCreateTime,beginCreateTime)
                .lt(endCreateTime!=null,User::getCreateTime,endCreateTime)
                ;
        return Result.success(userService.page(rowPage,wrapper));
    }
    @ApiOperation("修改 根据id")
//    @PreAuthorize("hasAuthority('admin')")
    @PutMapping()
    public Result updateById(@RequestBody User userChange){
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String password = passwordEncoder.encode(userChange.getPassword());
        userChange.setPassword(password);
        userService.updateById(userChange);
        return Result.success();
    }
    @ApiOperation("删除 批量")
//    @PreAuthorize("hasAuthority('admin')")
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        LambdaQueryWrapper<RoleUser> roleUserLambdaQueryWrapper = new LambdaQueryWrapper<RoleUser>()
                .in(ids!=null,RoleUser::getUserId,ids)
                .ne(RoleUser::getUserId,1);//不能删除管理员
        roleUserService.remove(roleUserLambdaQueryWrapper);
        userService.removeByIds(ids);
        return Result.success();
    }

    @ApiOperation("查询 根据id")
//    @PreAuthorize("hasAuthority('admin')")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        return Result.success(userService.getById(id));
    }
    @ApiOperation("添加 用户")
//    @PreAuthorize("hasAuthority('admin')")
    @PostMapping()
    public Result save(@RequestBody User entity,Integer roleId){
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String password = passwordEncoder.encode(entity.getPassword());
        entity.setPassword(password);
        userService.save(entity);
        LambdaQueryWrapper<User>userLambdaQueryWrapper = new LambdaQueryWrapper<User>()
                .select().eq(User::getUsername,entity.getUsername());
        RoleUser roleUser = RoleUser.builder()
                .roleId(roleId)
                .userId(userService.getOne(userLambdaQueryWrapper).getId())
                .build();
        roleUserService.save(roleUser);
        return Result.success();
    }
}
